package main.com.smartagriculture.observer;

import main.com.smartagriculture.observer.Observer;
import main.com.smartagriculture.observer.Subject;
import main.com.smartagriculture.sensor.EnvironmentSensor;

import java.util.ArrayList;
import java.util.List;

public class ConcreteSubject implements Subject {
    private List<Observer> observers = new ArrayList<>();
    private EnvironmentSensor sensor;

    public ConcreteSubject(EnvironmentSensor sensor) {
        this.sensor = sensor;
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(sensor);
        }
    }

    public void setSensorData(String data) {
        sensor.setData(data);
        notifyObservers();
    }
}